package org.demo.honor.service

import org.demo.honor.integration.dto.LockMobileStockDTO
import org.demo.honor.integration.dto.MobileTypeDTO
import org.demo.honor.integration.dto.PaymentDTO
import org.demo.honor.integration.payment.PaymentServiceFeignClient
import org.demo.honor.integration.stock.StockManagementFeignClient
import org.demo.honor.pojo.entity.OrderEntity
import org.demo.honor.pojo.request.CreateOrderRequest
import org.demo.honor.repository.OrderInvoiceRepository
import org.demo.honor.repository.OrderRepository
import spock.lang.Specification

class OrderServiceSpec extends Specification {

    def orderService
    def orderRepository
    def stockManagementFeignClient
    def paymentServiceFeignClient
    def orderInvoiceRepository
    def notificationService

    def setup() {
        orderRepository = Mock(OrderRepository)
        stockManagementFeignClient = Mock(StockManagementFeignClient)
        paymentServiceFeignClient = Mock(PaymentServiceFeignClient)
        orderInvoiceRepository = Mock(OrderInvoiceRepository)
        notificationService = Mock(NotificationService)
        orderService = new OrderService(orderRepository, stockManagementFeignClient, paymentServiceFeignClient, orderInvoiceRepository, notificationService)

    }

    def "test retrieveOrder"() {
        given:
        OrderEntity orderEntity = new OrderEntity()
        orderEntity.setId(111)
        orderEntity.setMobileName("honor")
        orderRepository.findById(_) >> Optional.of(orderEntity)

        when:
        OrderEntity order = orderService.retrieveOrder(001)

        then:
        order.getId() == 111
        order.getMobileName() == "honor"
    }

    def "test createOrder"() {
        CreateOrderRequest createOrderRequest = new CreateOrderRequest(
                city: "city", count: 100, comment: "comment", customerId: "comment", mobileType: "honor-car", phoneNumber: "phoneNumber",
                province: "province", recipientName: "recipientName", shippingAddress: "shippingAddress", streetAddress: "streetAddress"
        );

        List<MobileTypeDTO> mockMobileTypeDTOS = new ArrayList<>()
        mockMobileTypeDTOS.add(new MobileTypeDTO("honor-car", BigDecimal.valueOf(21.59)))
        mockMobileTypeDTOS.add(new MobileTypeDTO("honor-car1", BigDecimal.valueOf(22.59)))
        stockManagementFeignClient.retrieveMobileTypeList() >> mockMobileTypeDTOS

        OrderEntity mockOrder = new OrderEntity(
                id: 11, comment: createOrderRequest.getComment(), salesTime: new Date(), count: createOrderRequest.getCount());
        orderRepository.save(_) >> mockOrder

        PaymentDTO mockPaymentDTO = new PaymentDTO(id: "id", orderId: "orderId")

        paymentServiceFeignClient.createPayment(_) >> mockPaymentDTO

        LockMobileStockDTO mockLockMobileStockDTO = LockMobileStockDTO.builder()
                .imeis(["10", "10", "10"])
                .build()
        stockManagementFeignClient.lockMobileStock(_) >> mockLockMobileStockDTO

        when:
        OrderEntity orderEntity = orderService.createOrder(createOrderRequest)

        then:
        orderEntity.getCount() == 100
        orderEntity.getComment() == "comment"
    }
}

